<template>
  <div class="home">
    <div class="home-title">
      <img src="../assets/img/ico.png" alt="" srcset="" />
      <p>用户数据</p>
    </div>
    <div class="user-sys">
      <div class="user-item">
        <img src="../assets/img/h1.png" alt="" srcset="" />
        <p>
          <span style="color: #e38737">{{ total }}</span>
          <span>总注册量</span>
        </p>
      </div>
      <div class="user-item">
        <img src="../assets/img/h2.png" alt="" srcset="" />
        <p>
          <span style="color: #2abe17">{{ total1 }}</span>
          <span>激活用户量</span>
        </p>
      </div>
      <div class="user-item">
        <img src="../assets/img/h3.png" alt="" srcset="" />
        <p>
          <span style="color: #4855ff">{{ total2 }}</span>
          <span>在线日活量</span>
        </p>
      </div>
      <div class="user-item">
        <img src="../assets/img/h4.png" alt="" srcset="" />
        <p>
          <span style="color: #f01f7d">{{ total3 }}</span>
          <span>今日达标量</span>
        </p>
      </div>
    </div>
    <div class="home-main">
      <div class="main-left">
        <div class="home-title">
          <img src="../assets/img/ico.png" alt="" srcset="" />
          <p>代币数据</p>
        </div>
        <div class="left-content">
          <p class="jog-txt">JOG币</p>
          <div class="list-box">
            <div class="list-item" v-for="(item, index) in list" :key="index">
              <div class="item-num" :style="{ color: item.color }">
                <div class="einp-box" v-if="item.type === 'price'">
                  <div class="column_box">
                    <span class="content">{{ item.num }}</span>
                    <span class="label">最新价</span>
                  </div>
                  <div class="column_box">
                    <span class="content">{{
                      freeUsers.averageprice?freeUsers.averageprice.toFixed(2):'--'
                    }}USDT</span>
                    <span class="label">今日平均价</span>
                  </div>
                </div>
                <span v-else-if="!item.edit">{{ item.num }}</span>

                <div class="einp-box" v-if="item.edit">
                  <div class="column_box flexBox">
                    <div class="flexBox">
                      <input type="text" v-model="rate" class="einp" />
                      <button @click="_setCalRatio()">设置</button>
                    </div>
                    <span class="label">结算浮动分配比率</span>
                  </div>

                  <div class="column_box">
                    <span class="content">{{
                      freeUsers.controlprice&&freeUsers.controlprice.toFixed(2)
                    }}USDT</span>
                    <span class="label">调控后价格</span>
                  </div>

                </div>
              </div>
              <p :style="{ background: item.color }">{{ item.name }}</p>
              <!-- <img @click="visible=true" v-if="item.edit" src="../assets/img/edit.png" alt="" srcset="" class="edit"> -->
            </div>
            <div class="list-item">
              <div style="display: flex; padding-top: 10px">
                <div class="column_box flexBox" style="margin-right: 20px">
                  <span class="content">{{
                    freeUsers.totalusers || "--"
                  }}</span>
                  <span class="label">零撸人数</span>
                </div>
                <div class="column_box flexBox">
                  <div class="flexBox">
                    <input
                      type="text"
                      v-model="freeUsers.totalamount"
                      class="einp"
                    />
                    <button @click="totalamountChange()">设置</button>
                  </div>
                  <span class="label">分配总量</span>
                </div>
              </div>
              <p :style="{ background: '#330ed3' }">当日零撸调控</p>
              <!-- <img @click="visible=true" v-if="item.edit" src="../assets/img/edit.png" alt="" srcset="" class="edit"> -->
            </div>

            <!-- <div class="list-item">
              <div style="display: flex; padding-top: 10px">
                <div class="column_box flexBox" style="margin-right: 20px">
                  <span class="content">{{
                    freeUsers.totalusers || "--"
                  }}</span>
                  <span class="label">今日预产总量</span>
                </div>
                <div class="column_box flexBox">
                  <div class="flexBox">
                    <input
                      type="text"
                      v-model="freeUsers.totalamount"
                      class="einp"
                    />
                    <button @click="totalamountChange()">设置</button>
                  </div>
                  <span class="label">调控释放量</span>
                </div>
              </div>
              <p :style="{ background: '#330ed3' }">当日释放代币总量调控</p>
            </div> -->
            <!-- <div class="list-item"></div> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  homeUserData,
  homeTokenData,
  getContract,
  shuiContract,
  setCalRatio,
  getFreeUsers,
  setFreeAmount,
} from "@/utils/api";
export default {
  name: "HomeView",
  data() {
    return {
      total: "--",
      total1: "--",
      total2: "--",
      total3: "--",
      redum: "--",
      list: [
        { name: "总量", num: "--", color: "#19BED9" },
        { name: "余量", num: "--", color: "#1987D9" },
        { name: "可领取", num: "--", color: "#BA0AC9" },
        { name: "未领取", num: "--", color: "#0ED388" },
        { name: "已销毁", num: "--", color: "#D91919" },
        { name: "总税费", num: "--", color: "#E65B10" },
        { name: "价格", num: "--", color: "#330ED3", type: "price" },
        { name: "结算平均价浮动比例", num: "--", color: "#330ED3", edit: true },
        { name: "燃料总量", num: "--", color: "#330ED3" },
      ],
      visible: false,
      formLabelWidth: 120,
      rate: "",
      freeUsers: {},
    };
  },
  mounted() {
    this._homeUserData();
    this.fetchFreeUsers();
  },
  methods: {
    async _homeUserData() {
      let user = await homeUserData({});
      if (user.response.errCode == 0) {
        this.total = user.data.Total.toLocaleString();
        this.total1 = user.data.Total1.toLocaleString();
        this.total2 = user.data.Total2.toLocaleString();
        this.total3 = user.data.Total3.toLocaleString();
      }

      let token = await homeTokenData({});

      this.list[0].num = Math.floor("100000000").toLocaleString();
      this.list[1].num = Math.floor(token.data.Surplus).toLocaleString();
      this.list[2].num = Math.floor(token.data.Redeem).toLocaleString();
      this.list[3].num = Math.floor(token.data.Profit).toLocaleString();
      this.list[6].num = token.data.Price + " USDT";
      this.list[7].num = token.data.Rate;
      this.rate = token.data.Rate;

      let Contract = await getContract();
      this.list[4].num = Math.floor(Contract).toLocaleString();
      let fee = await shuiContract();
      this.list[5].num = Math.floor(fee[0]).toLocaleString();
      this.list[8].num = Math.floor(fee[1]).toLocaleString() + " KTO";
    },
    async fetchFreeUsers() {
      const res = await getFreeUsers({});
      this.freeUsers = res.data;
      console.log(res);
    },
    async totalamountChange() {
      if (this.freeUsers.totalamount > 0) {
        const params = { exdata: parseFloat(this.freeUsers.totalamount) };
        const { data, response } = await setFreeAmount(params);
        if (response.errCode == 0) {
          this.$message.success("设置成功");
        }
      } else {
        this.$message.error("设置的数量要大于0");
      }
    },
    async _setCalRatio() {
      if (this.rate > 0) {
        const { data, response } = await setCalRatio({
          exdata: parseFloat(this.rate),
        });
        if (response.errCode == 0) {
          this.freeUsers = { ...this.freeUsers ,controlprice: data} 
          this.$message.success("设置成功");
        }
      } else {
        this.$message.error("设置的比率要大于0");
      }
    },
  },
};
</script>
<style scoped lang="scss">
.home {
  padding: 20px;
}
.home-title {
  display: flex;
  align-items: center;
  padding-bottom: 20px;
  p {
    margin-left: 10px;
    font-size: 24px;
    color: #333333;
  }
  img {
    width: 24px;
  }
}
.ren-box {
  position: relative;
  margin-top: 43px;
  img {
    width: 100%;
    display: block;
    margin: 0;
    padding: 0;
  }
  .ren-info {
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    width: 100%;
    height: 100%;
    padding-left: 5%;
    span {
      &:nth-child(1) {
        color: #333333;
        margin-bottom: 4px;
      }
      &:nth-child(2) {
        color: #00b4e1;
        font-size: 24px;
      }
    }
  }
}
.list-box {
  padding: 20px 20px 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .list-item {
    width: 31.8%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    height: 150px;
    background: #ffffff;
    box-shadow: 0px 0px 7px 0px rgba(176, 176, 176, 0.5);
    border-radius: 5px;
    overflow: hidden;
    padding-top: 14px;
    box-sizing: border-box;
    margin-bottom: 30px;
    position: relative;
    .edit {
      position: absolute;
      top: 5px;
      right: 5px;
      width: 24px;
      height: 24px;
    }
    .einp-box {
      display: flex;
      align-items: center;
      justify-content: center;
      button {
        height: 30px;
        background: #330ed3;
        border-bottom-right-radius: 4px;
        border-top-right-radius: 4px;
        color: #fff;
        width: 40px;
        font-size: 12px;
      }
    }
    .einp {
      border: #330ed3 1px solid;
      height: 30px;
      border-bottom-left-radius: 4px;
      border-top-left-radius: 4px;
      width: 80px;
      text-align: center;
      font-size: 20px;
      color: #f01e7d;
      box-sizing: border-box;
      outline: none;
    }
    p {
      background: #eee;
      height: 36px;
      text-align: center;
      line-height: 36px;
      width: 100%;
      color: #fff;
    }
    .item-num {
      height: 80px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      font-size: 24px;
      font-weight: bolder;
    }
  }
}
.home-main {
  display: flex;
  justify-content: space-between;
  .main-left {
    width: 100%;
    overflow: hidden;
    padding-top: 20px;
  }
  .main-right {
    width: 24%;
    padding-top: 20px;
    display: flex;
    flex-direction: column;
  }
  .left-content {
    background: #fff;
    border-radius: 5px;
    .jog-txt {
      background: #6622ee;
      height: 44px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 20px;
      border-radius: 5px 5px 0 0;
      color: #fff;
    }
  }
  .right-content {
    background: #fff;
    border-radius: 5px;
    flex: 1;
    box-sizing: border-box;
    padding: 12px 20px;
  }
}
.flexBox {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
}
.column_box {
  display: flex;
  flex-direction: column;
  margin:0 30px;
  // border: 1px solid #dedede;

  span {
    font-size: 20px;
    font-weight: bolder;
    color: rgb(51, 14, 211);
  }
  .label {
    font-size: 16px;
    color: #999;
    margin-top: 10px;
    text-align: center;
  }
  button {
    height: 30px;
    background: #330ed3;
    border-bottom-right-radius: 4px;
    border-top-right-radius: 4px;
    color: #fff;
    width: 40px;
    font-size: 12px;
  }
}
.user-sys {
  display: flex;
  justify-content: space-between;
  .user-item {
    width: 24%;
    background: #fff;
    border-radius: 5px;
    height: 126px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 2%;
    box-sizing: border-box;
    p {
      display: flex;
      flex-direction: column;
      padding-left: 10%;
      flex: 1;
      span {
        &:nth-child(1) {
          font-size: 24px;
        }
        &:nth-child(2) {
          color: #999999;
          font-size: 16px;
        }
      }
    }
  }
}
</style>
